﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Text.RegularExpressions;

namespace VK.Comm
{
    //<system.web>
    //<httpModules>
    //  <add name="HttpModule" type="VK.Comm.HttpModule"/>
    //</httpModules>
    //<system.web>

    public class HttpModule : IHttpModule
    {
        public void Init(HttpApplication context)
        {
            context.BeginRequest += new EventHandler(context_BeginRequest);
        }

        private void context_BeginRequest(object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication)sender;
            HttpContext context = application.Context;

            //News_View_3.aspx
            string domainName = context.Request.Path.Split(new char[1]{'/'})[1];//context.Request.Url.Host;
            //以上两段代码为 得到假的请求的文件名

            //Regex regex = new Regex("([a-zA-Z0-9]).tiangs.com"); //用正则来验证是否合理
            Regex regex = new Regex(@"Dealer(\d+)");
            if (regex.IsMatch(domainName))
            {
                Match match = regex.Match(domainName);
                string domain = match.Groups[1].Value; //用来得到正则组中的数字，也就是当前请求的ID值
                if (domain == "567")
                {
                    context.RewritePath("~/BrandList", false); //将虚路径重写为新的正确的文件存在的路径，并指定其ID，也就是查询字符串
                }
            }
        }

        public void Dispose()
        {
        }
    }
}
